package LC_plus;

public class 面试题0204分割链表 {
    public ListNode partition(ListNode head, int x) {
        ListNode smallHead = new ListNode(-1);
        ListNode greatHead = new ListNode(-1);
        ListNode smallCur = smallHead;
        ListNode greatCur = greatHead;
        ListNode cur = head;
        while (cur != null) {
            if (cur.val < x) {
                smallCur.next = cur;
                smallCur = smallCur.next;
            } else {
                greatCur.next = cur;
                greatCur = greatCur.next;
            }
            cur = cur.next;
        }
        smallCur.next = greatHead.next;
        //由于greatCur是合并链表的最后一个节点，所以它要指向null
        greatCur.next = null;
        return smallHead.next;
    }
}
